'use strict';
// Declare app level module which depends on filters, and services
var app= angular.module('myApp', ['ngRoute']);
app.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: 'loginCtrl'});
  $routeProvider.when('/home', {templateUrl: 'partials/index_.html', controller: 'homeCtrl'});
  $routeProvider.when('/user', {templateUrl: 'partials/user.html', controller: 'homeCtrl'});
  $routeProvider.when('/group', {templateUrl: 'partials/group.html', controller: 'groupCtrl'});
  $routeProvider.when('/event', {templateUrl: 'partials/event.html', controller: 'eventCtrl'});
  $routeProvider.when('/statistic', {templateUrl: 'partials/statistic.html', controller: 'statCtrl'});
  $routeProvider.when('/user_detail/:id', {templateUrl: 'partials/user_detail.html', controller: 'user_detail_Ctrl'});
  $routeProvider.when('/group_detail/:id', {templateUrl: 'partials/group_detail.html', controller: 'group_detail_Ctrl'});
  $routeProvider.when('/event_detail/:id', {templateUrl: 'partials/event_detail.html', controller: 'event_detail_Ctrl'});
  $routeProvider.when('/admin', {templateUrl: 'partials/admin.html', controller: 'adminCtrl'});
  $routeProvider.when('/admin_', {templateUrl: 'partials/admin.html', controller: 'adminCtrl'});
  $routeProvider.when('/map', {templateUrl: 'partials/map.html', controller: 'mapCtrl'});
  $routeProvider.when('/admin_detail/:id', {templateUrl: 'partials/admin_detail.html', controller: 'admin_detail_Ctrl'});
  $routeProvider.when('/interest_detail/:id', {templateUrl: 'partials/interest_detail.html', controller: 'interest_detail_Ctrl'});
  $routeProvider.when('/interest', {templateUrl: 'partials/interest.html', controller: 'interestCtrl'});
   $routeProvider.when('/interest_', {templateUrl: 'partials/interest.html', controller: 'interestCtrl'});
  $routeProvider.otherwise({redirectTo: '/login'});
}]);


app.config(function($httpProvider) { 
    //Enable cross domain calls
    $httpProvider.defaults.useXDomain = true;

    //Remove the header used to identify ajax call  that would prevent CORS from working
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
});

app.filter('startFrom', function() {
    return function(input, start) {
        if(input) { 
            start = +start; //parse to int
            return input.slice(start);
        }
        return [];
        
    }
    
});



app.directive('backButton', function(){
    return {
      restrict: 'A',

      link: function(scope, element, attrs) {
        element.bind('click', goBack);

        function goBack() {
          history.back();
          scope.$apply();
        }
      }
    }
});

app.directive('ngConfirmClick', [
                                 function(){
                                     return {
                                         link: function (scope, element, attr) {
                                             var msg = attr.ngConfirmClick || "Are you sure?";
                                             var clickAction = attr.confirmedClick;
                                             element.bind('click',function (event) {
                                                 if ( window.confirm(msg) ) {
                                                     scope.$eval(clickAction)
                                                 }
                                             });
                                         }
                                     };
                             }])

app.run(function($rootScope, $location, loginService){
	var routespermission=['/home'];  //route that require login
	$rootScope.$on('$routeChangeStart', function(){
		if( routespermission.indexOf($location.path()) !=-1)
		{
			
			 
			var connected=loginService.islogged();
			//connected.then(function(msg){
				//if(!msg.data) $location.path('/login');
			//});
		}
	});
});